package com.hwadee.teachingprocessmanagementsystem.controller.teacher;

import com.alibaba.fastjson.JSONObject;
import com.hwadee.teachingprocessmanagementsystem.dto.student.StudentCoursewareDto;
import com.hwadee.teachingprocessmanagementsystem.dto.teacher.TeacherCourseStandardDto;
import com.hwadee.teachingprocessmanagementsystem.dto.teacher.TeacherCoursewareDto;
import com.hwadee.teachingprocessmanagementsystem.dto.teacher.TeacherScheduleInfoDto;
import com.hwadee.teachingprocessmanagementsystem.dto.teacher.TeacherTalentPlanDto;
import com.hwadee.teachingprocessmanagementsystem.entity.Student;
import com.hwadee.teachingprocessmanagementsystem.entity.Teacher;
import com.hwadee.teachingprocessmanagementsystem.service.base.MajorService;
import com.hwadee.teachingprocessmanagementsystem.service.teacher.TeacherService;
import com.hwadee.teachingprocessmanagementsystem.util.result.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * @Description: 教学基础信息控制器，负责教师教学基础信息的请求
 * @Author: Jukomu
 * @Package: com.hwadee.teachingprocessmanagementsystem.controller.student
 * @Project: TeachingProcessManagementSystem
 * @name: TeachingDataController
 * @Date: 2024/7/12-9:00
 * @Filename: TeachingDataController
 */
@RestController("TeacherTeachingDataController")
@RequestMapping("teacher/teaching-data")
public class TeachingDataController {
    @Autowired
    private MajorService majorService;

    @Autowired
    private TeacherService teacherService;


    /**
     * 培养方案接口
     *
     * @return
     */
    @RequestMapping("talent-plan")
    public DataResult<JSONObject> talentPlans() {
        TeacherTalentPlanDto teacherTalentPlanDto = this.teacherService.getTeacherTalentPlanDto();
        return DataResult.successByData(teacherTalentPlanDto);
    }

    /**
     * 授课课程信息接口
     *
     * @return
     */
    @RequestMapping("course-info")
    public DataResult courseInfo() {
        return DataResult.succ();
    }

    /**
     * 课程标准接口
     *
     * @return
     */
    @RequestMapping("materials/course-standard")
    public DataResult courseStandard(@RequestParam("teacher-number") String teacherNumber) {

        Teacher teacher = new Teacher();
        teacher.setTeacherId(Integer.parseInt(teacherNumber));
        TeacherCourseStandardDto teacherCourseStandardDto = this.teacherService.getTeacherCourseStandardDto(teacher);
        return DataResult.successByData(teacherCourseStandardDto);
    }

    /**
     * 教学计划接口
     *
     * @return
     */
    @RequestMapping("materials/teaching-plan")
    public DataResult teachingPlan() {
        return DataResult.succ();
    }

    /**
     * 课件管理接口
     *
     * @return
     */
    @RequestMapping("materials/courseware-management")
    public DataResult coursewareManagement(@RequestParam("teacher-number") String teacherNumber) throws IOException {
        Teacher teacher = new Teacher();
        teacher.setTeacherId(Integer.valueOf(teacherNumber));
        TeacherCoursewareDto teacherCoursewareDto = this.teacherService.getTeacherCoursewareDto(teacher);
        return DataResult.successByData(teacherCoursewareDto);
    }

    /**
     * 教师信息查询接口
     *
     * @return
     */
    @RequestMapping("teacher-info")
    public DataResult teacherInfo() {
        return DataResult.succ();
    }

    /**
     * 课表查询接口
     *
     * @return
     */
    @RequestMapping("schedule-info")
    public DataResult scheduleInfo(@RequestParam("teacher-number") String teacherNumber) {
        Teacher teacher = new Teacher();
        teacher.setTeacherId(Integer.parseInt(teacherNumber));
        TeacherScheduleInfoDto teacherScheduleInfo = this.teacherService.getTeacherScheduleInfo(teacher);
        return DataResult.successByData(teacherScheduleInfo);
    }

    /**
     * 新建文件夹
     * @param teacherNumber
     * @param dir
     * @param dirName
     * @param courseId
     * @return
     */
    @GetMapping("materials/courseware-management/create/directory")
    public DataResult createCoursewareDirectory(@RequestParam("teacher-number") String teacherNumber,
                                      @RequestParam("dir") String dir,
                                      @RequestParam("dirName") String dirName,
                                      @RequestParam("courseId") String courseId) {
        System.out.println(teacherNumber);
        System.out.println(dir);
        System.out.println(dirName);
        System.out.println(courseId);
        String path = "src/main/resources/static/courseware/" + courseId;
        if (Objects.equals(dir, "root")) {
            path = path + "/" + dirName;
        } else {
            path = path + "/" + dir + "/" + dirName;
        }
        System.out.println(path);
        boolean b = new File(path).mkdirs();
        if (b) {
            return DataResult.succ();
        }
        return DataResult.err();
    }

    /**
     * 删除文件/文件夹
     * @param teacherNumber
     * @param dir
     * @param fileName
     * @param courseId
     * @return
     */
    @GetMapping("materials/courseware-management/delete/directory")
    public DataResult deleteCoursewareDirectory(@RequestParam("teacher-number") String teacherNumber,
                                                @RequestParam("dir") String dir,
                                                @RequestParam("fileName") String fileName,
                                                @RequestParam("courseId") String courseId){
        System.out.println(teacherNumber);
        System.out.println(dir);
        System.out.println(fileName);
        System.out.println(courseId);
        String path = "src/main/resources/static/courseware/" + courseId + dir + "/" + fileName;

        System.out.println(path);
        boolean b = new File(path).delete();
        if (b) {
            return DataResult.succ();
        }
        return DataResult.err();
    }


    @PostMapping("materials/courseware-management/upload")
    public DataResult handleFileUpload(@RequestParam("file") MultipartFile file,
                                                   @RequestParam("path") String path,
                                                   @RequestParam("fileName") String fileName,
                                                   @RequestParam("courseId") String courseId) {
        try {
            // 文件保存逻辑
            String filePath = "src/main/resources/static/courseware/" + courseId + "/" + path + "/" + fileName;
            Path uploadPath = Paths.get(filePath);
            Files.createDirectories(uploadPath.getParent());
            Files.write(uploadPath, file.getBytes());
        } catch (IOException e) {
            return DataResult.err();
        }
        return DataResult.succ();
    }
}
